package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.exartisan.adapter.GalleryAlbumAdapter;
import com.exartisan.bean.Album;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.NewParamBuilder;
import com.exartisan.network.NewResponseTranslater;
import com.exartisan.network.NewRestConnector;
import com.exartisan.util.DataHolder;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;

public class FragmentGallary extends SherlockFragment {
	 private ListView listView;
     private ArrayList<Album> alAlbum;
     private ProgressBar prBar;
     private Button btnRetry;
     private GalleryAlbumAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_gallery, container, false);
        LinearLayout layout_bar = (LinearLayout) rootView.findViewById(R.id.layout_bar);
		layout_bar.setVisibility(View.GONE);
		RelativeLayout layout_topbar = (RelativeLayout) rootView.findViewById(R.id.layout_topbar);
		layout_topbar.setVisibility(View.GONE);
		listView = (ListView) rootView.findViewById(R.id.lvEventContent);
		prBar = (ProgressBar) rootView.findViewById(R.id.pro_gallery);
		btnRetry = (Button) rootView.findViewById(R.id.btnRetryGallery);
		
		listView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), true, true));
		
		btnRetry.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				getData();
			}
		});
		
		if (alAlbum == null) getData();
		else showGallery();
		
        return rootView;
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	
    	if (alAlbum != null) for (Album a : alAlbum) {
			if (a.getMediaID()==DataHolder.getAlbumID()) a.setViews(a.getViews() + 1);
		}
    	
    	if (adapter != null) {
    		adapter.flushLikeCmtCount();
    		adapter.notifyDataSetChanged();
    	}
    }
    
    
    public void showGallery(){
    
    	btnRetry.setVisibility(View.GONE);
    	prBar.setVisibility(View.GONE);
    	listView.setVisibility(View.VISIBLE);
    	adapter = new GalleryAlbumAdapter(getSherlockActivity(), alAlbum);
    	
		listView.setAdapter(adapter);
		
    }
    
    private void getData() {
		if (NetworkUtility.checkNetworkState(getSherlockActivity())) {
			btnRetry.setVisibility(View.GONE);
	    	prBar.setVisibility(View.VISIBLE);
	    	listView.setVisibility(View.GONE);
	    	
			NewRestConnector.post("", NewParamBuilder
					.BuildParams(NewParamBuilder.BuildAlbumData()),
					new AsyncHttpResponseHandler() {
						@Override
						public void onSuccess(int arg0, String response) {
							if (response == null){
								btnRetry.setVisibility(View.VISIBLE);
						    	prBar.setVisibility(View.GONE);
						    	listView.setVisibility(View.GONE);
						    	return;
							}
							super.onSuccess(arg0, response);
							if (NewResponseTranslater.checkSuccess(response)&&getSherlockActivity()!=null) {
								alAlbum = NewResponseTranslater.getAlbum(response);
								showGallery(); 
							} else {
								btnRetry.setVisibility(View.VISIBLE);
						    	prBar.setVisibility(View.GONE);
						    	listView.setVisibility(View.GONE);
							}

						}

						@Override
						public void onFailure(Throwable arg0, String arg1) {
							super.onFailure(arg0, arg1);
							btnRetry.setVisibility(View.VISIBLE);
					    	prBar.setVisibility(View.GONE);
					    	listView.setVisibility(View.GONE);
						}
					});
		} else {
			btnRetry.setVisibility(View.VISIBLE);
	    	prBar.setVisibility(View.GONE);
	    	listView.setVisibility(View.GONE);
	    	
			Toast.makeText(getSherlockActivity(), "Không có kết nối",
					Toast.LENGTH_SHORT).show();
		}
	}
    
 
}

